package com.example.adtest

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.activity.ComponentActivity
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import com.anythink.banner.api.ATBannerListener
import com.anythink.banner.api.ATBannerView
import com.anythink.core.api.ATAdInfo
import com.anythink.core.api.AdError
import com.anythink.interstitial.api.ATInterstitial
import com.anythink.interstitial.api.ATInterstitialListener
import com.anythink.nativead.api.ATNative
import com.anythink.nativead.api.ATNativeAdView
import com.anythink.nativead.api.ATNativeEventListener
import com.anythink.nativead.api.ATNativeNetworkListener
import com.anythink.rewardvideo.api.ATRewardVideoAd
import com.anythink.rewardvideo.api.ATRewardVideoListener
import com.anythink.splashad.api.ATSplashAd
import com.anythink.splashad.api.ATSplashAdExtraInfo
import com.anythink.splashad.api.ATSplashAdListener
import com.example.adtest.databinding.ActivityMainBinding
import java.text.SimpleDateFormat
import java.util.Date

class MainActivity : ComponentActivity() {
    private var binding: ActivityMainBinding? = null
    private var splashCount = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
        setClick()
    }
    private var bannerCount = 0
    private var atSplashAd: ATSplashAd? = null
    private var atRewardVideoAd: ATRewardVideoAd? = null
    private var atBannerView: ATBannerView? =null
    private var atNative: ATNative? =null
    private var loadScreen: ATInterstitial? =null
    private var screenCount = 0
    private var nativeCount = 0
    private var rewardCount = 0
    private val logLIst = ArrayList<LogBean>()
    private var allCount = 0

    private fun getTime(): String{
        val date: Date = Date()
        val sdf: SimpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        val currentDateTime: String = sdf.format(date)
        return currentDateTime
    }

    private fun setClick() {
        binding?.let {
            val logAdapter = LogAdapter(logLIst, this)
            it.logRecycler.adapter = logAdapter
            it.logRecycler.layoutManager = LinearLayoutManager(this)
            //刷新获取所有类型广告
            it.btnAllRequest.setOnClickListener { click ->
                //开屏
                atSplashAd = AdManager.loadSplash(this, object : ATSplashAdListener {
                    override fun onAdLoaded(p0: Boolean) {
                        val ecpm = atSplashAd?.checkAdStatus()?.atTopAdInfo?.ecpm
                        it.splashData.text = ecpm.toString()
                        logLIst.add(LogBean("开屏-成功",ecpm!!.toInt(),getTime()))
                        logAdapter.notifyDataSetChanged()
                    }

                    override fun onAdLoadTimeout() {
                        
                    }

                    override fun onNoAdError(p0: AdError?) {
                        logLIst.add(LogBean("开屏-失败",0,getTime()))
                    }

                    override fun onAdShow(p0: ATAdInfo?) {
                       splashCount++

                        it.splashCount.text= "开屏（$splashCount)"
                        allCount = allCount+atSplashAd?.checkAdStatus()?.atTopAdInfo?.ecpm!!.toInt()
                        it.allData.text = allCount.toString()
                    }

                    override fun onAdClick(p0: ATAdInfo?) {
                        
                    }

                    override fun onAdDismiss(p0: ATAdInfo?, p1: ATSplashAdExtraInfo?) {
                       it.splashView.visibility = View.GONE
                    }
                })
                //横幅
                atBannerView = AdManager.loadBanner(this,it.bannerView.width,it.bannerView.height)
                atBannerView!!.setBannerAdListener(object : ATBannerListener{
                    override fun onBannerLoaded() {
                        val ecpm = atBannerView?.checkAdStatus()?.atTopAdInfo?.ecpm
                        it.bannerData.text = ecpm.toString()
                        logLIst.add(LogBean("横幅-成功",ecpm!!.toInt(),getTime()))
                        logAdapter.notifyDataSetChanged()
                    }

                    override fun onBannerFailed(p0: AdError?) {
                        logLIst.add(LogBean("横幅-失败",0,getTime()))
                    }

                    override fun onBannerClicked(p0: ATAdInfo?) {
                        
                    }

                    override fun onBannerShow(p0: ATAdInfo?) {
                        bannerCount++
                        it.bannerCount.text="横幅（${bannerCount}）"
                        allCount = allCount+atBannerView?.checkAdStatus()?.atTopAdInfo?.ecpm!!.toInt()
                        it.allData.text = allCount.toString()
                    }

                    override fun onBannerClose(p0: ATAdInfo?) {
                        it.bannerView.removeAllViews()
                        atBannerView!!.destroy()
                    }

                    override fun onBannerAutoRefreshed(p0: ATAdInfo?) {
                        
                    }

                    override fun onBannerAutoRefreshFail(p0: AdError?) {
                        
                    }
                })
                //插屏
                loadScreen = AdManager.loadScreen(this)
                loadScreen!!.setAdListener(object : ATInterstitialListener{
                    override fun onInterstitialAdLoaded() {
                        val ecpm = loadScreen!!.checkAdStatus().atTopAdInfo.ecpm
                        it.screenData.text = ecpm.toString()

                        logLIst.add(LogBean("插屏-成功",ecpm!!.toInt(),getTime()))
                        logAdapter.notifyDataSetChanged()
                    }

                    override fun onInterstitialAdLoadFail(p0: AdError?) {
                        logLIst.add(LogBean("插屏-失败",0,getTime()))
                    }

                    override fun onInterstitialAdClicked(p0: ATAdInfo?) {
                        
                    }

                    override fun onInterstitialAdShow(p0: ATAdInfo?) {
                        screenCount++
                        it.screenCount.text == "插屏($screenCount)次"
                        allCount = allCount+loadScreen?.checkAdStatus()?.atTopAdInfo?.ecpm!!.toInt()
                        it.allData.text = allCount.toString()
                    }

                    override fun onInterstitialAdClose(p0: ATAdInfo?) {
                        
                    }

                    override fun onInterstitialAdVideoStart(p0: ATAdInfo?) {
                        
                    }

                    override fun onInterstitialAdVideoEnd(p0: ATAdInfo?) {
                        
                    }

                    override fun onInterstitialAdVideoError(p0: AdError?) {
                        
                    }

                })
                //原生
                atNative = AdManager.loadNative(this, object : ATNativeNetworkListener {
                        override fun onNativeAdLoaded() {
                            val ecpm = atNative?.checkAdStatus()?.atTopAdInfo?.ecpm
                            it.nativeData.text = ecpm.toString()
                            logLIst.add(LogBean("原生-成功",ecpm!!.toInt(),getTime()))
                            logAdapter.notifyDataSetChanged()
                            atNative?.nativeAd?.setNativeEventListener(object : ATNativeEventListener{
                                override fun onAdImpressed(p0: ATNativeAdView?, p1: ATAdInfo?) {
                                    
                                }

                                override fun onAdClicked(p0: ATNativeAdView?, p1: ATAdInfo?) {
                                    
                                }

                                override fun onAdVideoStart(p0: ATNativeAdView?) {
                                    nativeCount++
                                    it.nativeCount.text = "原生（${nativeCount}次）"
                                    allCount = allCount+atNative?.checkAdStatus()?.atTopAdInfo?.ecpm!!.toInt()
                                    it.allData.text = allCount.toString()
                                }

                                override fun onAdVideoEnd(p0: ATNativeAdView?) {
                                    
                                }

                                override fun onAdVideoProgress(p0: ATNativeAdView?, p1: Int) {
                                    
                                }
                            })
                        }

                        override fun onNativeAdLoadFail(p0: AdError?) {
                            logLIst.add(LogBean("原生-失败",0,getTime()))
                        }

                    }, width = it.bannerView.width, height = it.bannerView.height)
                //激励
                atRewardVideoAd = AdManager.loadReward(this)
                atRewardVideoAd!!.setAdListener(object : ATRewardVideoListener{
                    override fun onRewardedVideoAdLoaded() {
                        val ecpm = atRewardVideoAd?.checkAdStatus()?.atTopAdInfo?.ecpm
                        it.rewardData.text = ecpm.toString()
                        logLIst.add(LogBean("开屏-成功",ecpm!!.toInt(),getTime()))
                        logAdapter.notifyDataSetChanged()
                    }

                    override fun onRewardedVideoAdFailed(p0: AdError?) {
                        logLIst.add(LogBean("激励-失败",0,getTime()))
                    }

                    override fun onRewardedVideoAdPlayStart(p0: ATAdInfo?) {
                        rewardCount++
                        it.rewardCount.text = "激励（${rewardCount}）"
                        allCount = allCount+atRewardVideoAd?.checkAdStatus()?.atTopAdInfo?.ecpm!!.toInt()
                        it.allData.text = allCount.toString()
                    }

                    override fun onRewardedVideoAdPlayEnd(p0: ATAdInfo?) {
                       
                    }

                    override fun onRewardedVideoAdPlayFailed(p0: AdError?, p1: ATAdInfo?) {
                        
                    }

                    override fun onRewardedVideoAdClosed(p0: ATAdInfo?) {
                       
                    }

                    override fun onRewardedVideoAdPlayClicked(p0: ATAdInfo?) {
                  
                    }

                    override fun onReward(p0: ATAdInfo?) {
                       
                    }
                })
            }

            it.btnSplash.setOnClickListener { click ->
                it.splashView.visibility = View.VISIBLE
                atSplashAd?.show(this, it.splashView)
            }

            it.btnBanner.setOnClickListener{ c->
                it.bannerView.removeAllViews()
                it.bannerView.addView(atBannerView)
            }

            it.btnInterstitial.setOnClickListener{c->
                loadScreen?.show(this)
            }

            it.btnNative.setOnClickListener{c->
                it.bannerView.removeAllViews()
               atNative?.nativeAd?.renderAdContainer(it.bannerView,null)
            }

            it.btnReward.setOnClickListener{c->
                atRewardVideoAd?.show(this)
            }

            it.btnCloseBanner.setOnClickListener { c->
                it.bannerView.removeAllViews()
            }
        }
    }




    override fun onDestroy() {
        super.onDestroy()
        binding = null
    }
}
